// pages/list/list.js
Page({
  data:{
    companylist:[],
    companyindex:0,
    companyid:0,
    companyname:'',

    title:'',

    companyidarr:[],
    salaryindex:1,
    salarylist:['月薪','时薪'],

    phone:'',

    provinceid:1946,
    cityid:1990,
    cityindex:5,
    provinceindex:18,
    provincelist:[],
    citylist:[],
    provinceidarr:[],
    cityidarr:[],
    sexlist:['不限','男','女'],
    sexindex:0,

     salary1:0,
     salary2:0,
     salary3:0,
     hourse:260,
     JobDescription:'',
     InterviewDescription:'',
     Treatment:'',
     minage:18,
     maxage:45,
     needqty:30,

     MonthShow:true ,
     HourseShow:false,

     hiddenModal:true,
     logs:[],
     sessionKey:'',
     openid:''
  },
  onLoad:function(options){
    var that=this;
    //获取公司
  wx.request({
      url:'https://lendgiant.cn/explabor/getCompanylist',
      data: {},
      header:  {'Content-Type': 'application/json'},
      success: function(res) {
        that.data.companylist=[];
        that.data.companyidarr=[];
        that.data.companyindex=0;
      for( var i=0; i<res.data.list.length;i++){
          that.data.companylist.push( res.data.list[i].name) ;
          that.data.companyidarr.push(res.data.list[i].id);
      }

       that.setData({
            companylist: that.data.companylist,
            companyidarr:that.data.companyidarr,
            companyid:that.data.companyidarr[0]
       }) ;

      }
    });


//获取地址省份
     wx.request({
      url:'https://lendgiant.cn/index/getAreas',
      data: {},
      header: {'Content-Type': 'application/json'},
      success: function(res) {
        that.data.provincelist=[];
        that.data.provinceidarr=[];
      for( var i=0; i<res.data.list.length;i++){
          that.data.provincelist.push( res.data.list[i].name)
          that.data.provinceidarr.push(res.data.list[i].id);
      }
       that.setData({
           provincelist: that.data.provincelist,
           provinceidarr: that.data.provinceidarr
       })
      }
    })

  //获取城市
     wx.request({
      url:'https://lendgiant.cn/index/getAreas',
      data: {
         'parentid': 1946
      },
      header: {'Content-Type': 'application/json'},
      success: function(res) {
        that.data.citylist=[];
        that.data.cityidarr=[];
      for( var i=0; i<res.data.list.length;i++){
          that.data.citylist.push( res.data.list[i].name);
          that.data.cityidarr.push(res.data.list[i].id)
      }
       that.setData({
           citylist: that.data.citylist
       });

      }
    })


  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
bindPickerChangeSex:function(e){
  this.setData({
    sexindex:e.detail.value,

  })
},
   bindPickerChange: function(e) {
   // console.log('picker公司更改，携带值为', e.detail.value)
    this.setData({
      companyindex: e.detail.value,
      companyid:this.data.companyidarr[e.detail.value],
      companyname:this.data.companylist[e.detail.value]
    })

  },

  bindPickerChangeProvince:function(e){
    this.setData({
      provinceindex:e.detail.value,
      provinceid:this.data.provinceidarr[e.detail.value],
    });

    var that=this;
   wx.request({
      url:'http://lendgiant.cn/index/getAreas',
      data: {
         'parentid': that.data.provinceid
      },
      header: {'Content-Type': 'application/json'},
      success: function(res) {
        that.data.citylist=[];
        that.data.cityidarr=[];
      for( var i=0; i<res.data.list.length;i++){
          that.data.citylist.push( res.data.list[i].name);
          that.data.cityidarr.push(res.data.list[i].id)
      }
       that.setData({
           citylist: that.data.citylist
       });
      }
    })

  },
  bindPickerChangeCity:function(e){
    this.setData({
      cityindex: e.detail.value,
      cityid:this.data.cityidarr[e.detail.value]
    });

  },
   bindPickerChangeSalary: function(e) {
     if(e.detail.value==1){
        this.setData({
            MonthShow:true ,
            HourseShow:false,
        })
     }else{
        this.setData({
            MonthShow:false ,
            HourseShow:true,
        })
     }
    this.setData({
      salaryindex: e.detail.value,
    })
  },
  listernerAge2Change:function(e){
    this.data.minage=e.detail.value
  },
  listernerAge1Change:function(e){
    this.maxage=e.detail.value
  },
  listernerNeedQtyChange:function(e){
    this.data.needqty=e.detail.value
  },

  listenersalaryInput1: function(e) {
      this.data.salary1 = e.detail.value;
  },
  listenersalaryInput2: function(e) {
      this.data.salary2 = e.detail.value;
  },
   listenersalaryInput3: function(e) {
      this.data.salary3 = e.detail.value;
  },

  listenerPhoneInput:function(e){
    this.data.phone=e.detail.value;
  },
  charChangeJobDescription:function(e){
    this.setData({
      JobDescription:e.detail.value
    })
  },
  charChangeInterviewDescription:function(e){
    this.setData({
      InterviewDescription:e.detail.value
    })
  },
  charChangeTreatment:function(e){
      this.setData({
        Treatment:e.detail.value
      })
  },
   listernerTitleChange:function(e){
       this.data.title=e.detail.value
         console.log(e.detail.value)
   },
  saveJobOrder:function(){
    var that=this;
  if(that.data.title==''){
     that.setData({
           msg:'请输入标题！',
             hiddenModal:!that.data.hiddenModal,
         });
        return false;
}
if(that.data.needqty<0){
    that.setData({
           msg:'请输入正确人数！',
             hiddenModal:!that.data.hiddenModal,
         });
        return false;
}
 if(that.data.companyindex<0){
    that.setData({
           msg:'请选择招聘企业！',
             hiddenModal:!that.data.hiddenModal,
         });
        return false;
}
var reg=new RegExp('^([0-9])+(\.[0-9]+)?$');
 var r=new RegExp('^([0-9]{11,12})?$');

if(that.data.salaryindex==0){
  if( !reg.test(that.data.salary1) || that.data.salary1<=0){
      that.setData({
            msg:'请正确填写月薪范围！',
            hiddenModal:!that.data.hiddenModal,
      });
          return false;
  }
  if(!reg.test(that.data.salary2) || that.data.salary2<=0){
      that.setData({
            msg:'请正确填写月薪范围！',
            hiddenModal:!that.data.hiddenModal,
      });
          return false;
  }

  if(that.data.salary1>that.data.salary2){
     that.setData({
            msg:'最低工资不能大于最高工资！',
            hiddenModal:!that.data.hiddenModal,
      });
          return false;
  }
}
  if(that.data.salaryindex==1){
    if(  !reg.test(that.data.salary3) || that.data.salary3<=0){
        that.setData({
              msg:'请正确填写每小时工资！',
              hiddenModal:!that.data.hiddenModal,
        });
            return false;
    }
    if(  !reg.test(that.data.hourse) || that.data.hourse<=0){
        that.setData({
              msg:'请正确填写每月工作小时数！',
              hiddenModal:!that.data.hiddenModal,
        });
            return false;
    }
  }

if(that.data.JobDescription==undefined ||that.data.JobDescription.length==0){
   that.setData({
              msg:'请填写招聘要求！',
              hiddenModal:!that.data.hiddenModal,
    });
      return false;
}
if(that.data.InterviewDescription==undefined || that.data.InterviewDescription.length==0){
   that.setData({
              msg:'请填写应聘需要的资料！',
              hiddenModal:!that.data.hiddenModal,
    });
      return false;
}
if(that.data.Treatment==undefined || that.data.Treatment.length==0){
   that.setData({
              msg:'请填写工作其他福利待遇！',
              hiddenModal:!that.data.hiddenModal,
    });
      return false;
}

    if(!r.test(that.data.phone)){
         that.setData({
           msg:'请输入11位手机号或者12位座机号！'+that.data.phone,
             hiddenModal:!that.data.hiddenModal,
         });
        return false;
    }

     wx.request({
       url:'https://lendgiant.cn/explabor/saveJobOrder',
      data: {
        'companyname':that.data.companyname,
        'title':that.data.title,
         'provinceid': that.data.provinceid ,
         'cityid':that.data.cityid,
         'companyid':that.data.companyid,
         'salary1':that.data.salary1,
         'salary2':that.data.salary2,
         'salary3':that.data.salary3,
         'hourse':that.data.hourse,
         'phone':that.data.phone,
         'minage':that.data.minage,
         'maxage':that.data.maxage,
         'needqty':that.data.needqty,
         'sex':that.data.sexindex,
         'jobdescription':that.data.JobDescription,
         'interviewdescription':that.data.InterviewDescription,
         'treatment':that.data.Treatment,
          'salarytype':that.data.salaryindex
      },
        method: 'POST',
     header:{"Content-Type":"application/x-www-form-urlencoded"},
      success: function(res) {
      // if(!res.data.result){
       //  that.setData({
        //   hiddenModal:!that.data.hiddenModal,
      //     msg:res.data.msg
      //   })
      // }

    wx.showToast({
      title: '保存成功',
      icon: 'success',
      duration: 10000
    });

    setTimeout(function(){
      wx.hideToast()
    },2000);
      //   wx.showModal({
      //      title: '提示',
     //       content: '保存成功',
      //      showCancel: false
      //    })

     },
     fail:function(res){
          wx.showToast({
        title: '网络异常，保存失败',
        icon: 'loading',
        duration: 10000
    });

    setTimeout(function(){
      wx.hideToast()
    },1000);
     }
     })

  },

   bindViewTap: function() {
    wx.navigateTo({
           url: '../logs/logs',
           success: function(res){
             console.log("chenggong")
           },
           fail:function(res){
             console.log("fail")
           }
         })
  },
  bindReflashCompany:function(){
    var that=this;
  wx.request({
      url:'http://lendgiant.cn/explabor/getCompanylist',
      data: {},
      header:  {'Content-Type': 'application/json'},
      success: function(res) {
        that.data.companylist=[];
        that.data.companyidarr=[];
        that.data.companyindex=0;
      for( var i=0; i<res.data.list.length;i++){
          that.data.companylist.push( res.data.list[i].name) ;
          that.data.companyidarr.push(res.data.list[i].id);
      }
       that.setData({
            companylist: that.data.companylist,
            companyidarr:that.data.companyidarr
       }) ;
      }
    });
  },

  listenerConfirm:function(){
      this.setData({
           hiddenModal:!this.data.hiddenModal
         })
  },
  listenerCancel:function(){
      this.setData({
           hiddenModal:!this.data.hiddenModal
         })
  },
 onShareAppMessage: function () {
    return {
      title: '畅快发布您最新的招工信息',
      desc: '招工信息发布',
      path: '/pages/list/list'
    }
  }

})